package com.jingdianjichi.oss.adapter;

import com.jingdianjichi.oss.entity.FileInfo;
import com.jingdianjichi.oss.util.MinioUtil;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;


public class MinioStorageAdapter implements StorageAdapter {

    @Resource
    private MinioUtil minioUtil;
    @Override
    @SneakyThrows
    public void createBucket(String bucketName) throws Exception {
        minioUtil.createBucket(bucketName);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile multipartFile, String bucketName, String objectName) {
        minioUtil.createBucket(bucketName);
        if (objectName!=null) {
            minioUtil.uploadFile(multipartFile.getInputStream(), bucketName,
                    objectName+"/"+ multipartFile.getName());
        }else{
            minioUtil.uploadFile(multipartFile.getInputStream(), bucketName, multipartFile.getName());
        }
    }

    @Override
    @SneakyThrows
    public List<String> getAllBucket() {
        return minioUtil.getAllBucket();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucketName) {
        return minioUtil.getAllFile(bucketName);
    }

    @Override
    @SneakyThrows
    public InputStream downLoad(String bucketName, String objectName) {
        return minioUtil.downLoad(bucketName, objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucketName) {
        minioUtil.deleteBucket(bucketName);
    }

    @Override
    @SneakyThrows
    public void deleteFile(String bucketName, String objectName) {
        minioUtil.deleteFile(bucketName, objectName);
    }
}
